//
//  HSTimeLineModel.swift
//  HSStockChartDemo
//
//  Created by Hanson on 16/8/26.
//  Copyright © 2016年 hanson. All rights reserved.
//

import UIKit
import SwiftyJSON

class HSTimeLineModel: NSObject {
    
    var time: String = ""
    var price: CGFloat = 0
    var volume: CGFloat = 0
    var days: [String] = []
    var preClosePx: CGFloat = 0
    var avgPirce: CGFloat = 0
    var totalVolume: CGFloat = 0
    var trade: CGFloat = 0
    var rate: CGFloat = 0
//    var timeChartResult:TimeChartResult? = nil
//    class func getTimeLineModelArray(_ json: JSON) -> [HSTimeLineModel] {
//        var modelArray = [HSTimeLineModel]()
//        for (str, jsonData): (String, JSON) in json["chartlist"] {
//            print("\(str)  \(jsonData)")
//            let model = HSTimeLineModel()
//            model.time = Date.toDate(jsonData["time"].stringValue, format: "EEE MMM d HH:mm:ss z yyyy").toString("HH:mm")
//            model.avgPirce = CGFloat(jsonData["avg_price"].doubleValue)
//            model.price = CGFloat(jsonData["current"].doubleValue)
//            model.volume = CGFloat(jsonData["volume"].doubleValue)
//            model.days = (json["days"].arrayObject as? [String]) ?? [""]
//            modelArray.append(model)
//        }
//        return modelArray
//    }

    class func getTimeLineModelArray(_ json: JSON/*,timeChartResult:TimeChartResult*/) -> [HSTimeLineModel] {
        
        
//        let timeChartResult2 = Utils.AlamofireAsynchronousGETTimeChartData(stockCode: "000030"){
//            return $0
//        }
//        print(timeChartResult2)
        
        
        var modelArray = [HSTimeLineModel]()

                for (_, jsonData): (String, JSON) in json["chartlist"] {
                    let model = HSTimeLineModel()
                    model.time = Date.toDate(jsonData["time"].stringValue, format: "EEE MMM d HH:mm:ss z yyyy").toString("HH:mm")
                    model.avgPirce = CGFloat(jsonData["avg_price"].doubleValue)
                    model.price = CGFloat(jsonData["current"].doubleValue)
                    model.volume = CGFloat(jsonData["volume"].doubleValue)
                    model.days = (json["days"].arrayObject as? [String]) ?? [""]
                    modelArray.append(model)
                }
        
//        for data in dataArr {
//
//            let model = HSTimeLineModel()
//            model.time = Date.toDate(data.datetime, format: "HHmm").toString("HH:mm")
//
//            model.price = CGFloat(Double(data.price)!)
//            model.volume = CGFloat(Double(data.volumn)!)
//
//
//        }

        return modelArray
    }
//
//
//
    class func getTimeLineModelArray(result:TimeChartResult,/*_ json: JSON,*/ type: HSChartType, basicInfo: HSStockBasicInfoModel) -> [HSTimeLineModel] {
        var modelArray = [HSTimeLineModel]()
        var toComparePrice: CGFloat = 0
let arr=result.data
//        if (type == .timeLineForFiveday) {
//            //toComparePrice = CGFloat(json["chartlist"][0]["current"].doubleValue)
//            toComparePrice = CGFloat(Double(arr[0].price)!)
//
//        } else {
            toComparePrice = basicInfo.preClosePrice
//        }
        if (!arr.isEmpty){
        for ele in arr{
            let model = HSTimeLineModel()
            model.time = Date.toDate(ele.datetime, format: "HHmm").toString("HH:mm")
            model.avgPirce = CGFloat(Double(ele.volumnPrice)!/Double(ele.volumn)!)
            model.price = CGFloat(Double(ele.price)!)
            model.volume = CGFloat(Double(ele.volumn)!)
            model.rate = (CGFloat(Double(ele.price)!) - toComparePrice) / toComparePrice
            model.preClosePx = basicInfo.preClosePrice
//            model.days = (json["days"].arrayObject as? [String]) ?? [""]
            model.days = [""]
            modelArray.append(model)
        }
        }
        else{
            let model = HSTimeLineModel()
            modelArray.append(model)
        }
//        for ele in modelArray{
//            print("\(ele.time),\(ele.avgPirce),\(ele.price),\(ele.volume),\(ele.rate),\(ele.preClosePx)")
//        }
//        for (_, jsonData): (String, JSON) in json["chartlist"] {
//            let model = HSTimeLineModel()
//            model.time = Date.toDate(jsonData["time"].stringValue, format: "EEE MMM d HH:mm:ss z yyyy").toString("HH:mm")
//            model.avgPirce = CGFloat(jsonData["avg_price"].doubleValue)
//            model.price = CGFloat(jsonData["current"].doubleValue)
//            model.volume = CGFloat(jsonData["volume"].doubleValue)
//            model.rate = (model.price - toComparePrice) / toComparePrice
//            model.preClosePx = basicInfo.preClosePrice
//            model.days = (json["days"].arrayObject as? [String]) ?? [""]
//            modelArray.append(model)
//        }

        return modelArray
    }

//    class func getTimeLineModelArray(_ json: JSON, type: HSChartType, basicInfo: HSStockBasicInfoModel) -> [HSTimeLineModel] {
//        var modelArray = [HSTimeLineModel]()
//        let block = { (result:TimeChartResult?) in
//
//            if let re = result{
//            let arr = re.data
//            var toComparePrice: CGFloat = 0
//
//            if type == .timeLineForFiveday {
////                toComparePrice = CGFloat(json["chartlist"][0]["current"].doubleValue)
//                toComparePrice = CGFloat(Double(arr[0].price)!)
//
//            } else {
//                toComparePrice = basicInfo.preClosePrice
//            }
//
//            for (_, jsonData): (String, JSON) in json["chartlist"] {
//                let model = HSTimeLineModel()
//                model.time = Date.toDate(jsonData["time"].stringValue, format: "EEE MMM d HH:mm:ss z yyyy").toString("HH:mm")
//                model.avgPirce = CGFloat(jsonData["avg_price"].doubleValue)
//                model.price = CGFloat(jsonData["current"].doubleValue)
//                model.volume = CGFloat(jsonData["volume"].doubleValue)
//                model.rate = (model.price - toComparePrice) / toComparePrice
//                model.preClosePx = basicInfo.preClosePrice
//                model.days = (json["days"].arrayObject as? [String]) ?? [""]
//                modelArray.append(model)
//            }
//            for i in 0..<arr.count{
//                modelArray[i].time = Date.toDate(arr[i].datetime, format: "HHmm").toString("HH:mm")
//                modelArray[i].avgPirce = CGFloat(Double(arr[i].volumnPrice)!/Double(arr[i].volumn)!)
//                modelArray[i].price = CGFloat(Double(arr[i].price)!)
//                modelArray[i].volume = CGFloat(Double(arr[i].volumn)!)
//                modelArray[i].rate = (modelArray[i].price - toComparePrice) / toComparePrice
//                modelArray[i].preClosePx = basicInfo.preClosePrice
//            }
//            }
//
//        }
////        Utils.AlamofireAsynchronousGETTimeChartData(stockCode: "000030", callBack:block)
//                print()
//        print()
//        print()
//        print()
////                print(timeChartResult2)
//        print()
//        print()
//        print()
//
//
//
//
//
//
//        return modelArray
//    }
}

